package com.ruoyi.jiajia.controller;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.system.domain.JiajiaItemBigType;
import com.ruoyi.system.domain.JiajiaItemType;
import com.ruoyi.system.service.IJiajiaItemBigTypeService;
import com.ruoyi.system.service.IJiajiaItemTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 项目类型Controller
 * 
 * @author mrwang
 * @date 2024-05-08
 */
@Controller
@RequestMapping("/jiajia/item/type")
public class JiajiaItemTypeController extends BaseController
{
    private String prefix = "jiajia/itemType";

    @Autowired
    private IJiajiaItemTypeService jiajiaItemTypeService;

    @Autowired
    private IJiajiaItemBigTypeService jiajiaItemBigTypeService;

    @RequiresPermissions("system:type:view")
    @GetMapping()
    public String type(ModelMap mmap)
    {
        List<JiajiaItemBigType> itemTypeList = jiajiaItemBigTypeService.selectJiajiaItemBigTypeList(new JiajiaItemBigType());
        mmap.put("itemBigTypeList", itemTypeList);
        return prefix + "/type";
    }

    /**
     * 查询项目类型列表
     */
    @RequiresPermissions("system:type:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JiajiaItemType jiajiaItemType)
    {
        startPage();
        List<JiajiaItemType> list = jiajiaItemTypeService.selectJiajiaItemTypeList(jiajiaItemType);
        return getDataTable(list);
    }

    /**
     * 导出项目类型列表
     */
    @RequiresPermissions("system:type:export")
    @Log(title = "项目类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JiajiaItemType jiajiaItemType)
    {
        List<JiajiaItemType> list = jiajiaItemTypeService.selectJiajiaItemTypeList(jiajiaItemType);
        ExcelUtil<JiajiaItemType> util = new ExcelUtil<JiajiaItemType>(JiajiaItemType.class);
        return util.exportExcel(list, "项目类型数据");
    }

    /**
     * 新增项目类型
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<JiajiaItemBigType> itemTypeList = jiajiaItemBigTypeService.selectJiajiaItemBigTypeList(new JiajiaItemBigType());
        mmap.put("itemBigTypeList", itemTypeList);
        return prefix + "/add";
    }

    /**
     * 新增保存项目类型
     */
    @RequiresPermissions("system:type:add")
    @Log(title = "项目类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JiajiaItemType jiajiaItemType)
    {
        return toAjax(jiajiaItemTypeService.insertJiajiaItemType(jiajiaItemType));
    }

    /**
     * 修改项目类型
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        JiajiaItemType jiajiaItemType = jiajiaItemTypeService.selectJiajiaItemTypeById(id);
        mmap.put("jiajiaItemType", jiajiaItemType);
        List<JiajiaItemBigType> itemTypeList = jiajiaItemBigTypeService.selectJiajiaItemBigTypeList(new JiajiaItemBigType());
        List<JiajiaItemBigType> itemTypes = itemTypeList.stream().filter(rowVal -> StrUtil.equals(rowVal.getId().toString(), jiajiaItemType.getBigTypeId().toString())).collect(Collectors.toList());
        if(ObjectUtil.isNotEmpty(itemTypes)){
            itemTypes.get(0).setSelected(true);
        }
        mmap.put("itemBigTypeList", itemTypeList);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目类型
     */
    @RequiresPermissions("system:type:edit")
    @Log(title = "项目类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JiajiaItemType jiajiaItemType)
    {
        return toAjax(jiajiaItemTypeService.updateJiajiaItemType(jiajiaItemType));
    }

    /**
     * 删除项目类型
     */
    @RequiresPermissions("system:type:remove")
    @Log(title = "项目类型", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jiajiaItemTypeService.deleteJiajiaItemTypeByIds(ids));
    }
}
